<!-- 二级分类跳转页面 -->
<template>
  <div class='sub-category'>
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @filter-change="filterChange"/>
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @changeSort="changeSort"/>
        <!-- 列表 -->
        <ul>
          <li v-for="item in goodsList" :key="item.id" >
            <GoodsItem :goods="item" />
          </li>
        </ul>
        <!-- 数据加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData" />
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from './components/sub_bread.vue'
import SubFilter from './components/sub_filter.vue'
import SubSort from './components/sub_sort.vue'
import GoodsItem from './components/goods_item.vue'
import {findSubCategoryGoods} from '@/api/category'
import {ref, watch} from 'vue'
import { useRoute } from 'vue-router'
export default {
    name: 'SubCategory',
    components: {SubBread, SubFilter, SubSort, GoodsItem},
    setup() {
      const isAllChecked = ref(false)
      const loading = ref(false)
      const finished = ref(false)
      const route = useRoute()
      // 商品列表数据
      let goodsList = ref([])
      //请求参数
      let reqParams = {
        page: 1,
        pageSize: 20
      }
      
      const getData = () => {
        // 开启数据加载
        loading.value = true
        reqParams.categoryId = route.params.id
        //获取数据
        findSubCategoryGoods(reqParams).then(({result}) => {
          //如果有数据就追加数据
          if(result.items.length) {
            goodsList.value.push(...result.items)
            reqParams.page++

          } else {
            //没有数据代表加载完毕
            finished.value = true
          }
          //获取数据成功后关闭加载
          loading.value = false
        })
      }

      //侦听,切换二级分类重新加载
      watch(() => route.params.id, (newVal) => {
        if(newVal && `/category/sub/${newVal}` === route.path) {
          console.log(`/category/sub/${newVal}`);
          finished.value = false
          goodsList.value = []
          reqParams = {
            page: 1,
            pageSize: 20
          }
          
        }

      } )

      // 更改排序组件的筛选属性，重新发请求
      const changeSort = (sortParams) => {
        finished.value = false
        // 合并请求参数
        reqParams = {...sortParams, ...reqParams}
        reqParams.page = 1
        goodsList.value = []
      }
      //更改筛选组件的数据，，重新发请求
      const filterChange = (filterParams) => {
        finished.value = false
      // 合并请求参数，保留之前参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []

      }
      return {isAllChecked, loading, finished, getData, goodsList, changeSort, filterChange}
    }

}
</script>

<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}

</style>